package org.noote.libs.graphicbuffer;

public class ColorARGB {
	protected int _a=0, _r=0, _g=0, _b=0;
	
	public ColorARGB()
	{
		this(0);
	}
	public ColorARGB(int c)
	{
		_set(c);
	}
	public ColorARGB(int a, int r, int g, int b)
	{
		_set(a, r, g, b);
	}
	
	public int toInt()
	{
		return (_a<<24 | _r<<16 | _g<<8 | _b);
	}
	
	void _set(int a, int r, int g, int b)
	{
		_a=(short) ((a<0)?0:(a>255)?255:a);
		_r=(short) ((r<0)?0:(r>255)?255:r);
		_g=(short) ((g<0)?0:(g>255)?255:g);
		_b=(short) ((b<0)?0:(b>255)?255:b);
	}
	void _set(int c)
	{
		_a = (short)((c&0xFF000000)>>24);
		_r = (short)((c&0x00FF0000)>>16);
		_g = (short)((c&0x0000FF00)>>8);
		_b = (short)(c&0x000000FF);
	}

	void _limit()
	{
		_a=(_a<0)?0:(_a>255)?255:_a;
		_r=(_r<0)?0:(_r>255)?255:_r;
		_g=(_g<0)?0:(_g>255)?255:_g;
		_b=(_b<0)?0:(_b>255)?255:_b;
	}
	void _acc(ColorARGB c)
	{
		_a += c._a;
		_r += c._r;
		_g += c._g;
		_b += c._b;
	}
	void _dec(ColorARGB c)
	{
		_a -= c._a;
		_r -= c._r;
		_g -= c._g;
		_b -= c._b;
	}
	void _div(float d)
	{
		_a /= d;
		_r /= d;
		_g /= d;
		_b /= d;
	}

	public void add(ColorARGB c)
	{
		_acc(c);
		
		_limit();
	}
	
	public void sub(ColorARGB c)
	{
		_dec(c);
		
		_limit();
	}
	
	void sum(ColorARGB [] tab)
	{
		sum(tab, tab.length);
	}
	
	public void sum(ColorARGB [] tab, float divider)
	{
		_set(0, 0, 0, 0);
		for(ColorARGB c : tab)
		{
			_acc(c);
		}
		_div(divider);
	}
}
